home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / funcptr.arc / FUNCPTRS.C
Text File  |  1987-06-12  |  1KB  |  54 lines

  1. /*
  2.  * Turbo C demonstration program FUNCPTRS.C                   06/12/87
  3.  *
  4.  * This program demonstrates setting up an ARRAY of POINTERS to FUNCTIONS.
  5.  *
  6.  * int (*FNum[4])() is an array of 4 pointers to functions and is initialized
  7.  *                  with the 4 elements pointing to 4 different functions.
  8.  *
  9.  * Functions F1 .. F4 are never called directly.  Instead, they are called
  10.  * using with the statement:
  11.  *
  12.  *       (*FNum[x])();
  13.  *
  14.  * where "x" is an index into the array thus selecting the desired function.
  15.  *
  16.  *                                      by David W. Terry  71560,3550
  17.  */
  18.  
  19. void F1();
  20. void F2();    /* function prototypes - necessary for array initialization */
  21. void F3();
  22. void F4();
  23.  
  24. main() {
  25.   int  (*FNum[4])() = {F1,F2,F3,F4};     /* initialize function pointers */
  26.   char Ch;
  27.  
  28.   printf("This program demonstrates setting up an ARRAY of POINTERS to FUNCTIONS.");
  29.  
  30.   do {
  31.     printf("\n\nType 1,2,3,4 or Q: ");
  32.     Ch=getch();
  33.     printf("%c\n\n",Ch);
  34.     if (Ch >= '1' && Ch <= '4')
  35.       (*FNum[Ch-'1'])();          /* this one line will call all 4 funtions */
  36.   } while (toupper(Ch) != 'Q');
  37. }
  38.  
  39. void F1() {
  40.   printf("You're in Function 1");
  41. }
  42.  
  43. void F2() {
  44.   printf("You're in Function 2");
  45. }
  46.  
  47. void F3() {
  48.   printf("You're in Function 3");
  49. }
  50.  
  51. void F4() {
  52.   printf("You're in Function 4");
  53. }
  54.